<template>
  <el-tree-select
    v-model="selectedValue"
    :data="allDeptArr"
    filterable
    :props="{ value: 'groupEname', label: 'groupCname' }"
    v-on="$attrs"
    v-bind="$attrs"
    clearable
  />
</template>
<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { apiQueryDept } from '@/api/modules/EWPT.js'
const props = defineProps({
  modelValue: {
    type: String,
    default: '',
  },
})
const emits = defineEmits(['update:modelValue'])
let selectedValue: any = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emits('update:modelValue', val)
  },
})
// 流程发起部门
const allDeptArr = ref([])
// 查询所有流程发起部门
const getAllProcessesDept = () => {
  apiQueryDept().then((res) => {
    if (res.data.success === true) {
      //   allDeptArr.value = res.data.data || []
      allDeptArr.value = initDetpData(res.data.data)
      console.log('-------', allDeptArr.value)
    }
  })
}
const initDetpData = (data) => {
  const map = {}
  const treeData = []

  data.forEach((item) => {
    map[item.deptId] = { ...item, children: [] }
  })

  data.forEach((item) => {
    if (item.parentDeptId !== ' ') {
      map[item.parentDeptId].children.push(map[item.deptId])
    } else {
      treeData.push(map[item.deptId])
    }
  })

  return treeData
}
onMounted(() => {
  getAllProcessesDept()
})
</script>
